/*******************************************************************************
 * Copyright (c) 2012 Till Kolditz.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 * created on 10.10.2012 at 12:47:29
 * 
 *  Contributors:
 *      Till Kolditz
 *******************************************************************************/
package de.cc.yFilesParser.wizard;

import java.io.File;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import de.cc.yFilesParser.Activator;
import de.kolditz.common.ui.fields.BooleanField;
import de.kolditz.common.ui.fields.FieldComposite;
import de.kolditz.common.ui.fields.FileField;
import de.kolditz.common.ui.fields.LabelField;
import de.kolditz.common.util.IObservable;
import de.kolditz.common.util.IObserver;
import de.kolditz.common.util.IObserverSkeleton;

/**
 * @author Till Kolditz - Till.Kolditz@GoogleMail.com
 */
public class ConvertStartPage extends WizardPage
{
    public static final String PREF_SOURCE = "ConvertStartPage.Source"; //$NON-NLS-1$
    public static final String PREF_TARGET = "ConvertStartPage.Target"; //$NON-NLS-1$

    private ConvertWizard wizard;
    private FieldComposite fieldComp;
    private FileField ffSourceFile, ffTargetFile;
    private BooleanField bfSetStandard;
    private boolean sourceChanged = false, targetChanged = false;

    /**
     * @param pageName
     */
    public ConvertStartPage(ConvertWizard wizard)
    {
        super("ConvertStartPage");
        this.wizard = wizard;
        setTitle("Convert a yFile from yEd");
        setDescription("Convert a yEd yFile into a form usable in a control net (SML/NJ syntax)");
        setPageComplete(false);
    }

    @Override
    public void createControl(Composite parent)
    {
        parent.setLayoutDeferred(true);

        Composite area = new Composite(parent, SWT.NONE);
        area.setLayout(new GridLayout());
        area.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        fieldComp = new FieldComposite(area, SWT.NONE);
        fieldComp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

        ffSourceFile = new FileField(fieldComp, SWT.OPEN, "Source file:");
        // TODO add conversion support for e.g. GML, XGML
        /*
        ffSourceFile.setFilter(new String[] {"*.graphml;*.gml;*.xgml", "*.graphml", "*.gml", "*.xgml"},
                new String[] {"All yFiles (*.graphml;*.gml;*.xgml)", "GraphML (*.graphml)", "GML (*.gml)",
                        "Compressed GML (*.xgml)"}, 0);
        */
        ffSourceFile.setFilter(new String[] { "*.graphml" }, new String[] { "GraphML (*.graphml)" }, 0);

        ffTargetFile = new FileField(fieldComp, SWT.SAVE, "Target file:");
        ffTargetFile.setFilter(new String[] { "*.sml" }, new String[] { "SML/NJ file (*.sml)" }, 0);
        ffTargetFile.setOverwriteAsk(true);

        new LabelField(fieldComp, SWT.SEPARATOR | SWT.HORIZONTAL, "", false).setLayoutData(new GridData(SWT.FILL,
                SWT.BOTTOM, true, true));
        bfSetStandard = new BooleanField(fieldComp, SWT.NONE, "Set as standard", false);

        ffSourceFile.validate();
        ffTargetFile.validate();
        parent.setLayoutDeferred(false);
        setControl(area);
        setListeners();
        // the following is desired, because the logic in ConvertSettingsPage
        // depends on the source|targetChanged flags to be set
        applyStandard();
    }

    private void applyStandard()
    {
        IPreferenceStore store = Activator.getDefault().getPreferenceStore();
        if (!store.isDefault(PREF_SOURCE))
            ffSourceFile.setValue(store.getString(PREF_SOURCE));
        if (!store.isDefault(PREF_TARGET))
            ffTargetFile.setValue(store.getString(PREF_TARGET));
    }

    private void setListeners()
    {
        IObserver<String> observer = new IObserverSkeleton<String>()
        {
            @Override
            public void update(IObservable<String> source, String data)
            {
                if (source == ffSourceFile)
                    sourceChanged = true;
                else
                    if (source == ffTargetFile)
                        targetChanged = true;
                validate();
            }
        };
        ffSourceFile.registerObserver(observer);
        ffTargetFile.registerObserver(observer);
    }

    protected void validate()
    {
        boolean srcValid = getSourceFile() != null;
        boolean trgValid = getTargetFile() != null;
        setPageComplete(srcValid && trgValid);
    }

    /**
     * @return the complete source file path name
     */
    public File getSourceFile()
    {
        return ffSourceFile.getFile();
    }

    public File getTargetFile()
    {
        return ffTargetFile.getFile();
    }

    public Boolean isSetStandard()
    {
        return bfSetStandard.getValue();
    }

    public void saveAsStandard()
    {
        IPreferenceStore store = Activator.getDefault().getPreferenceStore();
        store.setValue(PREF_SOURCE, ffSourceFile.getValue());
        store.setValue(PREF_TARGET, ffTargetFile.getValue());
    }

    public void resetSourceChanged()
    {
        sourceChanged = false;
    }

    /**
     * @return the sourceChanged
     */
    public boolean isSourceChanged()
    {
        return sourceChanged;
    }

    public void resetTargetChanged()
    {
        targetChanged = false;
    }

    /**
     * @return the targetChanged
     */
    public boolean isTargetChanged()
    {
        return targetChanged;
    }

    @Override
    public void setVisible(boolean visible)
    {
        super.setVisible(visible);
        if (visible)
            wizard.getContainer().getShell().pack();
    }
}
